package com.gali.rabbit.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: gali
 * @Date: 2022-08-01 16:30
 * @Description:
 */
public class Consumer {
    // 队列名称
    public static final String QUEUE_NAME = "hello";
    //接收消息
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.186.128");
        connectionFactory.setUsername("gali");
        connectionFactory.setPassword("gali");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //声明 --接收消息
        DeliverCallback deliverCallback=(consumerTag,message)->{
            System.out.println(new String(message.getBody()));
        };
        //声明---取消消息
        CancelCallback cancelCallback=consumerTag ->{
            System.out.println("消息消费被中断");
        };
        //消费者消费消息
        //1. 消费哪个队列 2.消费成功后是否应答 3.消费者未成功消费的回调 4.消费者取消消费的回调
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}
